package neca.tmazo.vista.vistaGUI;

import java.awt.event.WindowEvent;

import javax.swing.JFrame;

import neca.tmazo.datasets.Album;
import neca.tmazo.datasets.Dataset;
import neca.tmazo.datasets.Music;
import neca.tmazo.datasets.Song;
import neca.tmazo.modelo.WindowEventListener;
import neca.tmazo.vista.VistaFacade;
import neca.tmazo.vista.WindowItems;

/**
 * 
 * @author neca
 *
 */
public class VistaFacadeGUI implements VistaFacade {
	
	private Ventana ventana;
	private WindowEventListener currentObserver;
	
	
	public VistaFacadeGUI()
	{
		//Cargamos las imagenes de disco a memoria, para que este retardo se produzca al principio
		//en vez de durante la ejecucion de la aplicacion
		ImageManager.loadImages();
		
		//Creamos una nueva ventana
		this.ventana=new Ventana();
		//Hacemos que, en cierre, se llame al metodo onExit() del observer.
		this.ventana.getJFrame().setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.ventana.getJFrame().addWindowListener(new java.awt.event.WindowAdapter() {
	        public void windowClosing(WindowEvent winEvt) {
	            onExit();
	        }
	    });
		//Centramos la ventana en pantalla
		this.ventana.getJFrame().setLocationRelativeTo( null );
		
	}
	@Override
	public void openWindowLogin(final WindowEventListener observer) 
	{
		this.currentObserver=observer;
		VentanaLogin vl = new VentanaLogin(observer);
		this.ventana.mostrar(vl);
	}

	@Override
	public void openWindowMiRecopilatorio(WindowEventListener observer,
			Music[] userMusic) 
	{
		this.currentObserver=observer;
		VentanaMiRecopilatorio v1=new VentanaMiRecopilatorio(observer,userMusic);
		this.ventana.mostrar(v1);
	}

	@Override
	public void openWindowFormularioRegistro(WindowEventListener observer) 
	{
		this.currentObserver=observer;
		VentanaFormularioRegistro v1=new VentanaFormularioRegistro(observer);
		this.ventana.mostrar(v1);

	}

	@Override
	public void openWindowBuscador(WindowEventListener observer) 
	{
		this.currentObserver=observer;
		Dataset dataset=new Dataset();
		dataset.add(WindowItems.BUSCADOR_FORMULARIO_TITULO, "");
		dataset.add(WindowItems.BUSCADOR_FORMULARIO_GENERO, "");
		dataset.add(WindowItems.BUSCADOR_FORMULARIO_FECHALANZAMIENTO, "");
		dataset.add(WindowItems.BUSCADOR_FORMULARIO_AUTOR, "");
		dataset.add(WindowItems.BUSCADOR_FORMULARIO_PRECIO, "");
		this.ventana.mostrar(new VentanaBuscador(observer,dataset));
	}

	@Override
	public void openWindowBuscador(WindowEventListener observer,
			Dataset lastData) 
	{
		this.currentObserver=observer;
		this.ventana.mostrar(new VentanaBuscador(observer, lastData));

	}

	@Override
	public void openWindowResultados(WindowEventListener observer,
			Music[] resultList) 
	{
		this.currentObserver=observer;
		this.ventana.mostrar(new VentanaResultados(observer, resultList));
	}

	@Override
	public void openWindowArtista(WindowEventListener observer,
			String artistName, Music[] artistMusic) 
	{
		this.currentObserver=observer;
		this.ventana.mostrar(new VentanaArtista(observer, artistName, artistMusic));
	}

	@Override
	public void openWindowCancion(WindowEventListener observer, Song song,boolean esComprada,boolean esPropiedad) 
	{
		this.currentObserver=observer;
		this.ventana.mostrar(new VentanaCancion(observer,song,esComprada,esPropiedad));

	}

	@Override
	public void openWindowAlbum(WindowEventListener observer, Album album,
			Song[] albumSongs,boolean esComprado,boolean esPropiedad) 
	{
		this.currentObserver=observer;
		this.ventana.mostrar(new VentanaAlbum(observer, album, albumSongs,esComprado,esPropiedad));
	}

	@Override
	public void openWindowSubirCancion(WindowEventListener observer) {
		this.currentObserver=observer;
		this.ventana.mostrar(new VentanaSubirCancion(observer));

	}

	@Override
	public void openWindowCrearAlbum(WindowEventListener observer) {
		this.currentObserver=observer;
		this.ventana.mostrar(new VentanaCrearAlbum(observer));
	}

	@Override
	public void openWindowCrearAlbum(WindowEventListener observer,
			Song[] selectedSongs, Dataset lastData) {
		this.currentObserver=observer;
		this.ventana.mostrar(new VentanaCrearAlbum(observer, selectedSongs,lastData));
	}

	@Override
	public void openWindowSeleccionCancion(WindowEventListener observer,
			Song[] yourMusic) {
		this.currentObserver=observer;
		this.ventana.mostrar(new VentanaAnadirCancionAlbum(observer, yourMusic));
	}

	@Override
	public void openWindowSubidaExitosa(WindowEventListener observer,
			Music uploadedItem) {
		this.currentObserver=observer;
		this.ventana.mostrar(new VentanaConfirmacion(observer, uploadedItem, false));
	}

	@Override
	public void openWindowCompraExitosa(WindowEventListener observer,
			Music bougthItem) {
		this.currentObserver=observer;
		this.ventana.mostrar(new VentanaConfirmacion(observer, bougthItem, true));
	}
	
	@Override
	public void openWindowErrorFatal(WindowEventListener observer, String texto) {
		this.currentObserver=observer;
		this.ventana.mostrar(new VentanaErrorFatal(observer, texto));
	}
	@Override
	public void openWindowHoyRecomendamos(WindowEventListener observer,
			Music[] recommendedMusic) {
		this.currentObserver=observer;
		this.ventana.mostrar(new VentanaHoyRecomendamos(observer,recommendedMusic));
		
	}
	@Override
	public void openWindowMiBiblioteca(WindowEventListener observer,
			Music[] userMusic) {
		this.currentObserver=observer;
		this.ventana.mostrar(new VentanaMiBiblioteca(observer,userMusic));
		
	}
	@Override
	public void showInfo(boolean error, String texto) {
		this.ventana.showInfo(texto,error);
	}
	@Override
	public void showInfo(boolean error, String texto,int cad) {
		this.ventana.showInfo(texto,error,cad);
	}
	private void onExit(){
		this.currentObserver.onExit();
	}
	@Override
	public void notifyWindowChange() {
		//Se ha solicitado un cambio de ventana. Hasta que este se produzca, mostramos loading
		this.ventana.hidePanel();		
	}

}
